package pres.wchen.oa.cor.shiro.web.wx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import pres.wchen.oa.cor.shiro.web.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/1/16 16:20
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */

@Controller
@RequestMapping("/wx")
public class WxCallBackController extends BaseController{
    Logger log = LoggerFactory.getLogger(WxCallBackController.class);
    private static final Logger logger = LoggerFactory.getLogger(WxCallBackController.class);


    @GetMapping("/callBack")
    public void wxCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception{

        log.info("**************************微信回调事件 开始**********************");
        String reqParams = read(request.getInputStream());
        //微信回调处理
        log.info(reqParams.toString());
        log.info("**************************微信回调事件 结束**********************");
        response.getWriter().append("");
    }


    public static String read(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[512];
            int len;
            while((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            return new String(baos.toByteArray(), 0, baos.size(), "utf-8");
        } catch (Exception var4) {
            var4.printStackTrace();
            return "";
        }
    }





    public void doLogin2(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map map = new HashMap();
        System.out.println("**************************回调事件**********************");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println("signature:" + signature);
        System.out.println("timestamp:" + timestamp);
        System.out.println("nonce:" + nonce);
        System.out.println("echostr:" + echostr);
        map.put("echostr",echostr);

        PrintWriter pw = response.getWriter();
        pw.append(echostr);
        pw.flush();
        pw.close();
    }






}
